home *** CD-ROM | disk | FTP | other *** search
- Path: digex.net!not-for-mail
- From: brettb@cpcug.org (H Brett Bolen)
- Newsgroups: comp.arch.embedded,comp.lang.c
- Subject: Re: Using malloc of C on embedded system?
- Date: 5 Apr 1996 11:59:40 -0500
- Organization: Walrus Consulting, Germantown MD USA
- Message-ID: <4k3jhs$k5m@cpcug.org>
- References: <4jml7h$cbc@castor.usc.edu> <mark.stephens-0104961340270001@mstephens.gsfc.nasa.gov>
- NNTP-Posting-Host: cpcug.org
-
-
- In article <mark.stephens-0104961340270001@mstephens.gsfc.nasa.gov>,
- mark stephens <mark.stephens@gsfc.nasa.gov> wrote:
- >You also might consider having many "mallocs" for each type of data
- >structure you have. I use a simple array of structures and a free list
- >composted of bytes. When a structure is allocated, it's corresponding bit
- >is set in the free/used list. Deallocation is the reverse. Structures
- >are allocated by liner search of the free list.
- >
- >The restrictions are you have to know the maximum number of structures
- >used at any one time. If you can know this, you will not run out of heap
- >and/or have the heap fractionated. You run out of heap and your systems
- >runs out on you!
- >
- >mark
-
- I've coded up a simular module. Basically we wanted to dynamically
- allocate strucs, without using heap. I coded it for my own use,
- but it's found it's way into a lot of code. A major use is to
- keep track of TCP clients in a real-time system.
-
- My module's interface is something like this:
-
- ------------------------------------------------------------ begin com_dtab.h
-
- [...]
-
- typedef struct {
- unsigned int used[4];
- int entry_size;
- int entry_last;
- int alloc_index;
- int iter_index;
- void *buf;
- } DTAB_TABLE;
-
-
-
- /*****
- ** Dynamic Table
- *****/
- int DTAB_create( DTAB_TABLE *dtab, int ent_sz, int ent_max, void *table);
- void *DTAB_alloc( DTAB_TABLE *dtab);
- void DTAB_free( DTAB_TABLE *dtab, void *entry);
- int DTAB_used( DTAB_TABLE *dtab, void *entry);
- void *DTAB_first( DTAB_TABLE *dtab);
- void *DTAB_next( DTAB_TABLE *dtab);
- void DTAB_print( DTAB_TABLE *dtab, char *banner);
-
-
- ------------------------------------------------------------ end com_dtab.h
-
-
- For each array to be allocated from, an extra struct ( DTAB for Dynamic
- table) is kept with the inuse flags.
-
- One thing that made it easy to use is iteration using _first() and
- _next():
-
- it = DTAB_first();
- while ( it) {
- process( it);
- it = DTAB_next();
- }
-
-
- Most functions are less than 5 lines of code. I think it had 62 SLOC --
- It shouldn't be too hard to re-engineer ( Although I lost a a few
- hair figuring out pointer subtraction).
-
-
- b\253 | Take Chances, Make Mistakes
- brettb@cpcug.org | Get Messy
- brett bolen | - Ms Frizzle - MSB
- Walrus Consulting | http://www.cpcug.org/user/brettb
-
-
- --
- b\253 | Take Chances, Make Mistakes
- brettb@cpcug.org | Get Messy
- brett bolen | - Ms Frizzle - MSB
- Walrus Consulting | http://www.cpcug.org/user/brettb
-